<?php

declare (strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Model;

use Hyperf\Database\Model\SoftDeletes;
use Hyperf\Database\Query\Builder;
class Information extends Model
{
    use SoftDeletes;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected ?string $table = 'information';
    public static $typeList = [1 => '人找车', 2 => '车找人', 3 => '顺风车', 4 => '求职招聘', 5 => '租房', 6 => '售房', 7 => '广告', 8 => '公益'];
    public static $allTypeList = [1 => '人找车', 2 => '车找人', 3 => '顺风车', 4 => '求职', 5 => '租房', 6 => '售房', 7 => '广告', 8 => '公益', 9 => '招聘', 10 => '求租'];
    public static $statusList = [1 => '进行中', -1 => '已结束', 2 => '待支付'];
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }
    public function imgs()
    {
        return $this->belongsToMany(Upload::class, 'information_img', 'information_id', 'img_id');
    }
    public function getTypeTextAttribute()
    {
        return static::$typeList[$this->type] ?? '未知';
    }
    public function getAllTypeTextAttribute()
    {
        return static::$allTypeList[$this->all_type] ?? '未知';
    }
    public function getStatusTextAttribute()
    {
        return static::$statusList[$this->status] ?? '已结束';
    }
    /**
     * 广告信息
     * @return \Hyperf\Database\Model\Relations\HasOne
     */
    public function ad()
    {
        return $this->hasOne(InformationAd::class, 'information_id');
    }
    /**
     * 车找人信息
     * @return \Hyperf\Database\Model\Relations\HasOne
     */
    public function cp()
    {
        return $this->hasOne(InformationCP::class, 'information_id');
    }
    /**
     * 顺风车信息
     * @return \Hyperf\Database\Model\Relations\HasOne
     */
    public function freeRider()
    {
        return $this->hasOne(InformationFreeRider::class, 'information_id');
    }
    /**
     * 售房信息
     * @return \Hyperf\Database\Model\Relations\HasOne
     */
    public function houseSales()
    {
        return $this->hasOne(InformationHouseSales::class, 'information_id');
    }
    /**
     * 求职信息
     * @return \Hyperf\Database\Model\Relations\HasOne
     */
    public function jobWanted()
    {
        return $this->hasOne(InformationJobWanted::class, 'information_id');
    }
    /**
     * 人找车信息
     * @return \Hyperf\Database\Model\Relations\HasOne
     */
    public function pc()
    {
        return $this->hasOne(InformationPC::class, 'information_id');
    }
    /**
     * 公益信息
     * @return \Hyperf\Database\Model\Relations\HasOne
     */
    public function publicGood()
    {
        return $this->hasOne(InformationPublicGood::class, 'information_id');
    }
    /**
     * 招聘信息
     * @return \Hyperf\Database\Model\Relations\HasOne
     */
    public function recruitment()
    {
        return $this->hasOne(InformationRecruitment::class, 'information_id');
    }
    /**
     * 出租信息
     * @return \Hyperf\Database\Model\Relations\HasOne
     */
    public function rent()
    {
        return $this->hasOne(InformationRent::class, 'information_id');
    }
    /**
     * 求租信息
     * @return \Hyperf\Database\Model\Relations\HasOne
     */
    public function solicit()
    {
        return $this->hasOne(InformationSolicit::class, 'information_id');
    }
    public function scopeActive($q)
    {
        return $q->where('status', 1);
    }
    /**
     * 关键词搜索
     * @param $q
     * @param $keyword
     * @return Builder
     */
    public function scopeKeyword($q, $keyword)
    {
        /** @var Builder $q */
        return $q->where(function ($q) use($keyword) {
            $q->whereHas('user', function ($q) use($keyword) {
                $q->where('mobile', 'like', '%' . $keyword . '%');
                $q->orWhere('nickname', 'like', '%' . $keyword . '%');
            });
            $q->orWhereHas('ad', function ($q) use($keyword) {
                $q->where('content', 'like', '%' . $keyword . '%');
            });
            $q->orWhereHas('cp', function ($q) use($keyword) {
                $q->where('content', 'like', '%' . $keyword . '%');
            });
            $q->orWhereHas('freeRider', function ($q) use($keyword) {
                $q->where('content', 'like', '%' . $keyword . '%');
            });
            $q->orWhereHas('houseSales', function ($q) use($keyword) {
                $q->where('content', 'like', '%' . $keyword . '%');
            });
            $q->orWhereHas('jobWanted', function ($q) use($keyword) {
                $q->where('username', 'like', '%' . $keyword . '%');
                $q->orWhere('mobile', 'like', '%' . $keyword . '%');
            });
            $q->orWhereHas('pc', function ($q) use($keyword) {
                $q->where('content', 'like', '%' . $keyword . '%');
            });
            $q->orWhereHas('publicGood', function ($q) use($keyword) {
                $q->where('content', 'like', '%' . $keyword . '%');
            });
            $q->orWhereHas('recruitment', function ($q) use($keyword) {
                $q->where('name', 'like', '%' . $keyword . '%');
            });
            $q->orWhereHas('rent', function ($q) use($keyword) {
                $q->where('content', 'like', '%' . $keyword . '%');
                $q->orWhere('title', 'like', '%' . $keyword . '%');
            });
            $q->orWhereHas('solicit', function ($q) use($keyword) {
                $q->where('content', 'like', '%' . $keyword . '%');
            });
        });
    }
    public function getInfoObject()
    {
        switch ($this->all_type) {
            //人找车
            case 1:
                $obj = $this->pc;
                break;
            //车找人
            case 2:
                $obj = $this->cp;
                break;
            //顺风车
            case 3:
                $obj = $this->freeRider;
                break;
            //求职
            case 4:
                $obj = $this->jobWanted;
                break;
            //租房
            case 5:
                $obj = $this->rent;
                break;
            //售房
            case 6:
                $obj = $this->houseSales;
                break;
            //广告
            case 7:
                $obj = $this->ad;
                break;
            //公益
            case 8:
                $obj = $this->publicGood;
                break;
            //招聘
            case 9:
                $obj = $this->recruitment;
                break;
            //求租
            case 10:
                $obj = $this->solicit;
                break;
            default:
                $obj = null;
                break;
        }
        return $obj;
    }
    public function getInfo()
    {
        $obj = $this->getInfoObject();
        if (!$obj) {
            return [];
        }
        //
        //        return $obj->info();
        switch ($this->all_type) {
            //人找车
            case 1:
                $data = ['content' => $obj->content ?? '', 'origin' => $obj->origin ?? '', 'origin_location' => ['lat' => $obj->origin_lat ?? '', 'lng' => $obj->origin_lng ?? ''], 'destination' => $obj->destination ?? '', 'destination_location' => ['lat' => $obj->destination_lat ?? '', 'lng' => $obj->destination_lng ?? ''], 'time' => $obj->time ?? '', 'number' => $obj->number ?? 0];
                break;
            //车找人
            case 2:
                $data = ['content' => $obj->content ?? '', 'origin' => $obj->origin ?? '', 'origin_location' => ['lat' => $obj->origin_lat ?? '', 'lng' => $obj->origin_lng ?? ''], 'destination' => $obj->destination ?? '', 'destination_location' => ['lat' => $obj->destination_lat ?? '', 'lng' => $obj->destination_lng ?? ''], 'time' => $obj->time ?? '', 'number' => $obj->number ?? 0];
                break;
            //顺风车
            case 3:
                $data = ['content' => $obj->content ?? '', 'origin' => $obj->origin ?? '', 'origin_location' => ['lat' => $obj->origin_lat ?? '', 'lng' => $obj->origin_lng ?? ''], 'destination' => $obj->destination ?? '', 'destination_location' => ['lat' => $obj->destination_lat ?? '', 'lng' => $obj->destination_lng ?? ''], 'time' => $obj->time ?? '', 'number' => $obj->number ?? 0];
                break;
            //求职
            case 4:
                $data = ['name' => $obj->username ?? '', 'sex' => $obj->sex ?? 3, 'sex_text' => $obj->sex_text, 'qualification' => $obj->qualification ?? '', 'qualification_text' => $obj->qualification_text ?? '', 'university' => $obj->university ?? '', 'birthday' => $obj->birthday ?? '', 'city_id' => $obj->city_id ?? '', 'city' => $obj->city->name ?? '', 'trade_id' => $obj->trade_id ?? '', 'trade' => $obj->trade->name ?? '', 'pay' => $obj->pay ?? '面议', 'work_experience' => $obj->work_experience ?? '', 'education' => $obj->education ?? '', 'content' => $obj->content ?? '', 'img_id' => $obj->img_id ?? '', 'img' => stripslashes(($obj->img->url ?? ''))];
                break;
            //租房
            case 5:
                $data = ['title' => $obj->title ?? '', 'address' => $obj->address ?? '', 'address_location' => ['lat' => $obj->lat ?? '', 'lng' => $obj->lng ?? ''], 'total' => $obj->total ?? 0.0, 'rent_type' => $obj->rent_type ?? '', 'rent_type_text' => $obj->rent_type_text, 'content' => $obj->content ?? ''];
                break;
            //售房
            case 6:
                $data = ['title' => $obj->title ?? '', 'city_id' => $obj->city_id ?? '', 'city' => $obj->city->name ?? '', 'village' => $obj->village ?? '', 'address' => $obj->address ?? '', 'address_location' => ['lat' => $obj->lat ?? '', 'lng' => $obj->lng ?? ''], 'area' => $obj->area ?? 0.0, 'total' => $obj->total ?? 0.0, 'house_type' => $obj->house_type ?? '', 'content' => $obj->content ?? ''];
                break;
            //广告
            case 7:
                $data = ['content' => $obj->content ?? ''];
                break;
            //公益
            case 8:
                $data = ['content' => $obj->content ?? ''];
                break;
            //招聘
            case 9:
                $data = ['name' => $obj->name ?? '', 'city_id' => $obj->city_id ?? '', 'city' => $obj->city->name ?? '', 'pay' => $obj->pay ?? '面议', 'duty' => $obj->duty ?? '', 'material_benefits' => $obj->material_benefits ? explode(',', $obj->material_benefits) : '', 'enterprise' => $obj->enterprise->name ?? ''];
                break;
            //求租
            case 10:
                $data = ['content' => $obj->content ?? ''];
                break;
            default:
                $data = [];
                break;
        }
        return $data;
    }
    /**
     * 接口返回
     * @param $user
     * @return array
     */
    public function toApi($user)
    {
        $count = 0;
        if ($user) {
            /** @var User $user */
            $count = $user->informationLikes()->where('information_id', $this->id)->count();
        }
        $data = ['id' => $this->id, 'views' => $this->views ?? 0, 'likes' => $this->likes ?? 0, 'likes_status' => $count > 0 ? 1 : -1, 'imgs' => [], 'username' => $this->username ?? '', 'mobile' => $this->mobile ?? '', 'info_type' => $this->all_type ?? '', 'info_type_text' => $this->type_text, 'user' => $this->user->nickname ?? '', 'header' => stripslashes(($this->user->header->url ?? '')), 'status' => $this->status ?? -1, 'status_text' => $this->status_text, 'location' => ['lat' => $this->lat ?? '', 'lng' => $this->lng ?? ''], 'distance' => $this->distance ?? '', 'create_time' => $this->create_time ? $this->create_time->toDateTimeString() : ''];
        if (in_array($this->type, [4, 5]) && $this->all_type) {
            $data['info_type_text'] .= '-' . $this->all_type_text;
        }
        $imgList = $this->imgs;
        foreach ($imgList as $img) {
            $data['imgs'][] = ['id' => $img->id, 'url' => stripslashes(($img->url ?? ''))];
        }
        $data = array_merge($data, $this->getInfo());
        return $data;
    }
    public function makeInfo()
    {
        switch ($this->all_type) {
            //人找车
            case 1:
                $obj = $this->pc()->make();
                break;
            //车找人
            case 2:
                $obj = $this->cp()->make();
                break;
            //顺风车
            case 3:
                $obj = $this->freeRider()->make();
                break;
            //求职
            case 4:
                $obj = $this->jobWanted()->make();
                break;
            //租房
            case 5:
                $obj = $this->rent()->make();
                break;
            //售房
            case 6:
                $obj = $this->houseSales()->make();
                break;
            //广告
            case 7:
                $obj = $this->ad()->make();
                break;
            //公益
            case 8:
                $obj = $this->publicGood()->make();
                break;
            //招聘
            case 9:
                $obj = $this->recruitment()->make();
                break;
            //求租
            case 10:
                $obj = $this->solicit()->make();
                break;
            default:
                $obj = null;
                break;
        }
        return $obj;
    }
    /**
     * 上/下架
     * @return $this
     */
    public function handle()
    {
        if ($this->status == 2) {
            return $this;
        }
        if ($this->status != 1) {
            $this->status = 1;
        } else {
            $this->status = -1;
        }
        return $this;
    }
    /**
     * 点赞
     * @param User $user
     * @return $this
     * @throws \Exception
     */
    public function giveTheThumbsUp(User $user)
    {
        if (!$user) {
            return $this;
        }
        $likes = $user->informationLikes()->where('information_id', $this->id)->first();
        if ($likes) {
            $likes->delete();
        } else {
            /** @var InformationLikes $likes */
            $likes = $user->informationLikes()->make();
            $likes->information()->associate($this);
            $likes->save();
        }
        return $this;
    }
    /**
     * 发布信息
     * @return $this
     */
    public function top()
    {
        return $this;
    }
}